package com.cat.action;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.cat.bean.common.Constant;
import com.cat.business.RoleBusinessService;
import com.cat.entity.TbRole;
import com.cat.entity.TbUser;
import com.cat.service.common.RoleService;
import com.cat.service.common.UserService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class RoleAction extends ActionSupport {
	
	@Resource
	private RoleBusinessService roleBService;
	@Resource
	private UserService userService;
	@Resource
	private RoleService roleService;
	private TbRole role;
	private List<TbRole> roleList;
	private boolean success;
	private String message;

	public String showRole() {

		return SUCCESS;
	}

	public String initRole() {
		if (role == null) {
			role = new TbRole();
		}
		roleList = roleBService.getRoleList(role);
		StringBuffer xml = new StringBuffer(
				"<?xml version='1.0' encoding='utf-8'?>");
		xml.append("<rows>");
		for (TbRole item : roleList) {
			xml.append("<row id='").append(item.getRoleId()).append("'>");
			xml.append("<cell>").append(item.getRoleName()).append("</cell>");
			xml.append("<cell></cell>");
			xml.append("</row>");
		}
		xml.append("</rows>");

		HttpServletResponse response = (HttpServletResponse) ActionContext
				.getContext().get(
						org.apache.struts2.StrutsStatics.HTTP_RESPONSE);
		try {
			response.setCharacterEncoding("UTF-8");
			response.getWriter().write(xml.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public String saveRole() {
		try {
			 roleBService.saveRole(role);
			 message=Constant.SUCCESS_MESSAGE;
			 success = true;
		} catch (Exception e) {
			e.printStackTrace();
			success = false;
		}
		return SUCCESS;
	}

	public String editRole() {
		try {
			role = roleBService.getRole(role.getRoleId());
			success = true;
		} catch (Exception e) {
			e.printStackTrace();
			success = false;
		}
		return SUCCESS;
	}

	public String deleteRole() {
		try {
			List<TbUser> userlist=userService.getList("userType", role.getRoleId());
			if(userlist!=null&&userlist.size()>0){
				success=false;
				message="该角色已在使用，不可删除!";
			}else{
				roleBService.deleteRole(role);
				success = true;
			}
		} catch (Exception e) {
			e.printStackTrace();
			success = false;
		}
		return SUCCESS;
	}


	public String checkRole() {
		// 根据当前输入值获取对象
		TbRole model = roleService.get("roleName", role.getRoleName());
		TbRole dbModel = null;
		message="";
		if (role.getRoleId() != null) {
			dbModel = roleService.get(role.getRoleId());
		}
		//新增
		if(dbModel==null){
			if(model!=null){
				message="该角色已存在，请重新添加!";
			}
		}else{
			//编辑
			if(!dbModel.getRoleName().equals(role.getRoleName())){
				if(model!=null){
					message="该角色已存在，请重新添加!";
				}
			}
		}
		return SUCCESS;
	}

	public TbRole getRole() {
		return role;
	}

	public void setRole(TbRole role) {
		this.role = role;
	}

	public List<TbRole> getRoleList() {
		return roleList;
	}

	public void setRoleList(List<TbRole> roleList) {
		this.roleList = roleList;
	}

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

}
